/*
 * Created: Author: Aléx Carvalho / Date: Mar 11, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-11 04:31:49 +0000 (Thu, 11 Jun 2015) $
 */
package com.cpm.view.zk.pane;

import com.cpm.Callback;
import com.cpm.view.IconKit;
import com.cpm.view.CSSClass;
import com.cpm.view.Element;
import com.cpm.view.zk.IconElement;
import com.cpm.view.zk.VerticalMenu;
import com.cpm.view.zk.applications.AbstractApplication;
import com.cpm.view.Icon;
import java.util.Optional;
import org.zkoss.zhtml.Div;
import org.zkoss.zhtml.impl.ContentTag;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Menu;
import org.zkoss.zul.Menubar;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Menupopup;

public class HeaderPane implements Element {

    private final Div div = new Div();
    public final int HEADER_HEIGHT = 50;
    private Optional<Callback<AbstractApplication>> callback = Optional.empty();
    
    public HeaderPane() {
        initComponents();
    }

    public void setCallback(Callback<AbstractApplication> callback) {
        this.callback = Optional.of(callback);
    }

    private void initComponents() {

        Icon icon = new Icon(IconKit.MENU, CSSClass.HEADER_MENU_ICON);
        IconElement menuButton = new IconElement(icon);

        ContentTag span = new ContentTag("span");
        span.setContent("CPM");
        span.setDynamicProperty("class", CSSClass.HEADER_TITLE);

        Div menuButtonDiv = new Div();
        menuButtonDiv.setId("vmenubutton");
        menuButtonDiv.setDynamicProperty("style", "height: 50px; width: 50px; cursor: pointer; border-right: 1px white solid;");
        menuButtonDiv.appendChild(menuButton.getElement());

        Menupopup menupopup = new Menupopup();

        Menu menu = new Menu("Bem vindo");
        menu.setIconSclass(IconKit.USER.getValue() + " " + CSSClass.HEADER_USER_ICON );
        menu.appendChild(menupopup);

        Menuitem exitItem = new Menuitem("Sair");
        menupopup.appendChild(exitItem);

        Menubar menubar = new Menubar();
        menubar.appendChild(menu);
        menubar.setStyle("top: 0px; position: absolute; right: 0; background: transparent !important; border-left: 1px solid white;");

        div.setDynamicProperty("height", HEADER_HEIGHT + "px");
        div.setDynamicProperty("class", CSSClass.HEADER);
        div.appendChild(menuButtonDiv);
        div.appendChild(span);
        div.appendChild(menubar);

        exitItem.addEventListener(Events.ON_CLICK, e -> {
            Executions.getCurrent().getSession().invalidate();
            Executions.sendRedirect(null);
        });
        
        VerticalMenu verticalMenu = new VerticalMenu();
        div.appendChild(verticalMenu.getElement());
        
        verticalMenu.setCallback(c -> {
            callback.ifPresent( ca ->  ca.accept(c));
        });
    }

    @Override
    public Component getElement() {
        return div;
    }
}
